<!doctype html>
<html class="default no-js">
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title>Compound.js - v0.1.2</title>
	<meta name="description" content="Documentation for Compound.js - v0.1.2">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="stylesheet" href="assets/css/main.css">
</head>
<body>
<header>
	<div class="tsd-page-toolbar">
		<div class="container">
			<div class="table-wrap">
				<div class="table-cell" id="tsd-search" data-index="assets/js/search.json" data-base=".">
					<div class="field">
						<label for="tsd-search-field" class="tsd-widget search no-caption">Search</label>
						<input id="tsd-search-field" type="text" />
					</div>
					<ul class="results">
						<li class="state loading">Preparing search index...</li>
						<li class="state failure">The search index is not available</li>
					</ul>
					<a href="index.html" class="title">Compound.js - v0.1.2</a>
				</div>
				<div class="table-cell" id="tsd-widgets">
					<div id="tsd-filter">
						<a href="#" class="tsd-widget options no-caption" data-toggle="options">Options</a>
						<div class="tsd-filter-group">
							<div class="tsd-select" id="tsd-filter-visibility">
								<span class="tsd-select-label">All</span>
								<ul class="tsd-select-list">
									<li data-value="public">Public</li>
									<li data-value="protected">Public/Protected</li>
									<li data-value="private" class="selected">All</li>
								</ul>
							</div>
							<input type="checkbox" id="tsd-filter-inherited" checked />
							<label class="tsd-widget" for="tsd-filter-inherited">Inherited</label>
							<input type="checkbox" id="tsd-filter-only-exported" />
							<label class="tsd-widget" for="tsd-filter-only-exported">Only exported</label>
						</div>
					</div>
					<a href="#" class="tsd-widget menu no-caption" data-toggle="menu">Menu</a>
				</div>
			</div>
		</div>
	</div>
	<div class="tsd-page-title">
		<div class="container">
			<ul class="tsd-breadcrumb">
				<li>
					<a href="globals.html">Globals</a>
				</li>
			</ul>
			<h1>Compound.js - v0.1.2</h1>
		</div>
	</div>
</header>
<div class="container container-main">
	<div class="row">
		<div class="col-8 col-content">
			<div class="tsd-panel tsd-typography">
				<a href="#compoundjs" id="compoundjs" style="color: inherit; text-decoration: none;">
					<h1>Compound.js</h1>
				</a>
				<p>A JavaScript SDK for Ethereum and the Compound Protocol. Wraps around <a href="https://github.com/ethers-io/ethers.js/">Ethers.js</a>. Works in the <strong>web browser</strong> and <strong>Node.js</strong>.</p>
				<p><a href="https://compound-finance.github.io/compound-js/docs/">Compound.js Documentation</a></p>
				<p>This SDK is in <strong>open beta</strong>, and is constantly under development. <strong>USE AT YOUR OWN RISK</strong>.</p>
				<a href="#ethereum-read-amp-write" id="ethereum-read-amp-write" style="color: inherit; text-decoration: none;">
					<h2>Ethereum Read &amp; Write</h2>
				</a>
				<p>JSON RPC based Ethereum <strong>read</strong> and <strong>write</strong>.</p>
				<a href="#read" id="read" style="color: inherit; text-decoration: none;">
					<h3>Read</h3>
				</a>
				<pre><code class="language-js"><span class="hljs-keyword">const</span> Compound = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;@compound-finance/compound-js&#x27;</span>); <span class="hljs-comment">// in Node.js</span>
<span class="hljs-keyword">const</span> cUsdtAddress = Compound.util.getAddress(Compound.cUSDT);

(<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{

  <span class="hljs-keyword">let</span> supplyRatePerBlock = <span class="hljs-keyword">await</span> Compound.eth.read(
    cUsdtAddress,
    <span class="hljs-string">&#x27;function supplyRatePerBlock() returns (uint)&#x27;</span>,
    [], <span class="hljs-comment">// [optional] parameters</span>
    {}  <span class="hljs-comment">// [optional] call options, provider, network, ethers.js &quot;overrides&quot;</span>
  );

  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;USDT supplyRatePerBlock:&#x27;</span>, supplyRatePerBlock.toString());

})().catch(<span class="hljs-built_in">console</span>.error);</code></pre>
				<a href="#write" id="write" style="color: inherit; text-decoration: none;">
					<h3>Write</h3>
				</a>
				<pre><code class="language-js"><span class="hljs-keyword">const</span> toAddress = <span class="hljs-string">&#x27;0xa0df350d2637096571F7A701CBc1C5fdE30dF76A&#x27;</span>;

(<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{

  <span class="hljs-keyword">const</span> trx = <span class="hljs-keyword">await</span> Compound.eth.trx(
    toAddress,
    <span class="hljs-string">&#x27;function send() external payable&#x27;</span>,
    [],
    {
      <span class="hljs-attr">value</span>: Compound._ethers.utils.parseEther(<span class="hljs-string">&#x27;1.0&#x27;</span>), <span class="hljs-comment">// 1 ETH</span>
      <span class="hljs-attr">provider</span>: <span class="hljs-built_in">window</span>.ethereum, <span class="hljs-comment">// in a web browser</span>
    }
  );

  <span class="hljs-keyword">const</span> toAddressEthBalance = <span class="hljs-keyword">await</span> Compound.eth.getBalance(toAddress);

})().catch(<span class="hljs-built_in">console</span>.error);</code></pre>
				<a href="#compound-protocol" id="compound-protocol" style="color: inherit; text-decoration: none;">
					<h2>Compound Protocol</h2>
				</a>
				<p>Simple methods for using the Compound protocol.</p>
				<pre><code class="language-js"><span class="hljs-keyword">const</span> compound = <span class="hljs-keyword">new</span> Compound(<span class="hljs-built_in">window</span>.ethereum); <span class="hljs-comment">// in a web browser</span>

<span class="hljs-comment">// Ethers.js overrides are an optional 3rd parameter for `supply`</span>
<span class="hljs-comment">// const trxOptions = { gasLimit: 250000, mantissa: false };</span>

(<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{

  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;Supplying ETH to the Compound protocol...&#x27;</span>);
  <span class="hljs-keyword">const</span> trx = <span class="hljs-keyword">await</span> compound.supply(Compound.ETH, <span class="hljs-number">1</span>);
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;Ethers.js transaction object&#x27;</span>, trx);

})().catch(<span class="hljs-built_in">console</span>.error);</code></pre>
				<a href="#install--import" id="install--import" style="color: inherit; text-decoration: none;">
					<h2>Install / Import</h2>
				</a>
				<p>Web Browser</p>
				<pre><code class="language-html"><span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text/javascript&quot;</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;https://cdn.jsdelivr.net/npm/@compound-finance/compound-js@latest/dist/browser/compound.min.js&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text/javascript&quot;</span>&gt;</span><span class="javascript">
  <span class="hljs-built_in">window</span>.Compound; <span class="hljs-comment">// or `Compound`</span>
</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span></code></pre>
				<p>Node.js</p>
				<pre><code>npm <span class="hljs-keyword">install</span> @<span class="hljs-keyword">compound</span>-finance/<span class="hljs-keyword">compound</span>-js</code></pre>
				<pre><code class="language-js"><span class="hljs-keyword">const</span> Compound = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;@compound-finance/compound-js&#x27;</span>);</code></pre>
				<a href="#more-code-examples" id="more-code-examples" style="color: inherit; text-decoration: none;">
					<h2>More Code Examples</h2>
				</a>
				<ul>
					<li><a href="https://github.com/compound-finance/compound-js/tree/master/examples">Node.js</a></li>
					<li><a href="https://compound-finance.github.io/compound-js/examples/web/">Web Browser</a></li>
				</ul>
				<p><a href="https://github.com/compound-finance/compound-js/tree/master/examples">To run, boot Ganache fork of mainnet locally</a></p>
				<a href="#instance-creation" id="instance-creation" style="color: inherit; text-decoration: none;">
					<h2>Instance Creation</h2>
				</a>
				<p>The following are valid Ethereum providers for initialization of the SDK.</p>
				<pre><code class="language-js"><span class="hljs-keyword">var</span> compound = <span class="hljs-keyword">new</span> Compound(<span class="hljs-built_in">window</span>.ethereum); <span class="hljs-comment">// web browser</span>

<span class="hljs-keyword">var</span> compound = <span class="hljs-keyword">new</span> Compound(<span class="hljs-string">&#x27;http://127.0.0.1:8545&#x27;</span>); <span class="hljs-comment">// HTTP provider</span>

<span class="hljs-keyword">var</span> compound = <span class="hljs-keyword">new</span> Compound(); <span class="hljs-comment">// Uses Ethers.js fallback mainnet (for testing only)</span>

<span class="hljs-keyword">var</span> compound = <span class="hljs-keyword">new</span> Compound(<span class="hljs-string">&#x27;ropsten&#x27;</span>); <span class="hljs-comment">// Uses Ethers.js fallback (for testing only)</span>

<span class="hljs-comment">// Init with private key (server side)</span>
<span class="hljs-keyword">var</span> compound = <span class="hljs-keyword">new</span> Compound(<span class="hljs-string">&#x27;https://mainnet.infura.io/v3/_your_project_id_&#x27;</span>, {
  <span class="hljs-attr">privateKey</span>: <span class="hljs-string">&#x27;0x_your_private_key_&#x27;</span>, <span class="hljs-comment">// preferably with environment variable</span>
});

<span class="hljs-comment">// Init with HD mnemonic (server side)</span>
<span class="hljs-keyword">var</span> compound = <span class="hljs-keyword">new</span> Compound(<span class="hljs-string">&#x27;mainnet&#x27;</span> {
  <span class="hljs-attr">mnemonic</span>: <span class="hljs-string">&#x27;clutch captain shoe...&#x27;</span>, <span class="hljs-comment">// preferably with environment variable</span>
});</code></pre>
				<a href="#constants-and-contract-addresses" id="constants-and-contract-addresses" style="color: inherit; text-decoration: none;">
					<h2>Constants and Contract Addresses</h2>
				</a>
				<p>Names of contracts, their addresses, ABIs, token decimals, and more can be found in <code>/src/constants.ts</code>. Addresses, for all networks, can be easily fetched using the <code>getAddress</code> function, combined with contract name constants.</p>
				<pre><code class="language-js"><span class="hljs-built_in">console</span>.log(Compound.DAI, Compound.ETH, Compound.cETH);
<span class="hljs-comment">// DAI, ETH, cETH</span>

<span class="hljs-keyword">const</span> cUsdtAddress = Compound.util.getAddress(Compound.cUSDT);
<span class="hljs-comment">// Mainnet cUSDT address. Second parameter can be a network like &#x27;ropsten&#x27;.</span></code></pre>
				<a href="#mantissas" id="mantissas" style="color: inherit; text-decoration: none;">
					<h2>Mantissas</h2>
				</a>
				<p>Parameters of number values can be plain numbers or their scaled up mantissa values. There is a transaction option boolean to tell the SDK what the developer is passing.</p>
				<pre><code class="language-js"><span class="hljs-comment">// 1 Dai</span>
<span class="hljs-keyword">await</span> compound.borrow(Compound.DAI, <span class="hljs-string">&#x27;1000000000000000000&#x27;</span>, { <span class="hljs-attr">mantissa</span>: <span class="hljs-literal">true</span> });

<span class="hljs-comment">// `mantissa` defaults to false if it is not specified or if an options object is not passed</span>
<span class="hljs-keyword">await</span> compound.borrow(Compound.DAI, <span class="hljs-number">1</span>, { <span class="hljs-attr">mantissa</span>: <span class="hljs-literal">false</span> });</code></pre>
				<a href="#transaction-options" id="transaction-options" style="color: inherit; text-decoration: none;">
					<h2>Transaction Options</h2>
				</a>
				<p>Each method that interacts with the blockchain accepts a final optional parameter for overrides, much like <a href="https://docs.ethers.io/ethers.js/v5-beta/api-contract.html#overrides">Ethers.js overrides</a>.</p>
				<pre><code class="language-js"><span class="hljs-comment">// The options object itself and all options are optional</span>
<span class="hljs-keyword">const</span> trxOptions = {
  mantissa,   <span class="hljs-comment">// Boolean, parameters array arg of 1 ETH would be &#x27;1000000000000000000&#x27; (true) vs 1 (false)</span>
  abi,        <span class="hljs-comment">// Definition string or an ABI array from a solc build</span>
  provider,   <span class="hljs-comment">// JSON RPC string, Web3 object, or Ethers.js fallback network (string)</span>
  network,    <span class="hljs-comment">// Ethers.js fallback network provider, &quot;provider&quot; has precedence over &quot;network&quot;</span>
  <span class="hljs-keyword">from</span>,       <span class="hljs-comment">// Address that the Ethereum transaction is send from</span>
  gasPrice,   <span class="hljs-comment">// Ethers.js override `Compound_ethers.utils.parseUnits(&#x27;10.0&#x27;, &#x27;gwei&#x27;)`</span>
  gasLimit,   <span class="hljs-comment">// Ethers.js override - see https://docs.ethers.io/ethers.js/v5-beta/api-contract.html#overrides</span>
  value,      <span class="hljs-comment">// Number or string</span>
  data,       <span class="hljs-comment">// Number or string</span>
  chainId,    <span class="hljs-comment">// Number</span>
  nonce,      <span class="hljs-comment">// Number</span>
  privateKey, <span class="hljs-comment">// String, meant to be used with `Compound.eth.trx` (server side)</span>
  mnemonic,   <span class="hljs-comment">// String, meant to be used with `Compound.eth.trx` (server side)</span>
};</code></pre>
				<a href="#api" id="api" style="color: inherit; text-decoration: none;">
					<h2>API</h2>
				</a>
				<p>The <a href="https://compound.finance/docs/api">Compound API</a> is accessible from Compound.js. The corresponding services are defined in the <code>api</code> namespace on the class.</p>
				<ul>
					<li><code>Compound.api.account</code></li>
					<li><code>Compound.api.cToken</code></li>
					<li><code>Compound.api.marketHistory</code></li>
					<li><code>Compound.api.governance</code></li>
				</ul>
				<p>The governance method requires a second parameter (string) for the corresponding endpoint shown in the <a href="https://compound.finance/docs/api#GovernanceService">documentation</a>.</p>
				<ul>
					<li><code>proposals</code></li>
					<li><code>voteReceipts</code></li>
					<li><code>accounts</code></li>
				</ul>
				<p>Here is an example for using the <code>account</code> endpoint. The <code>network</code> parameter in the request body is optional and defaults to <code>mainnet</code>.</p>
				<pre><code class="language-js"><span class="hljs-keyword">const</span> main = <span class="hljs-keyword">async</span> () =&gt; {
  <span class="hljs-keyword">const</span> account = <span class="hljs-keyword">await</span> Compound.api.account({
    <span class="hljs-string">&quot;addresses&quot;</span>: <span class="hljs-string">&quot;0xB61C5971d9c0472befceFfbE662555B78284c307&quot;</span>,
    <span class="hljs-string">&quot;network&quot;</span>: <span class="hljs-string">&quot;ropsten&quot;</span>
  });

  <span class="hljs-keyword">let</span> daiBorrowBalance = <span class="hljs-number">0</span>;
  <span class="hljs-keyword">if</span> (<span class="hljs-built_in">Object</span>.isExtensible(account) &amp;&amp; account.accounts) {
    account.accounts.forEach(<span class="hljs-function">(<span class="hljs-params">acc</span>) =&gt;</span> {
      acc.tokens.forEach(<span class="hljs-function">(<span class="hljs-params">tok</span>) =&gt;</span> {
        <span class="hljs-keyword">if</span> (tok.symbol === Compound.cDAI) {
          daiBorrowBalance = +tok.borrow_balance_underlying.value;
        }
      });
    });
  }

  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;daiBorrowBalance&#x27;</span>, daiBorrowBalance);
}

main().catch(<span class="hljs-built_in">console</span>.error);</code></pre>
				<a href="#build-for-nodejs-amp-web-browser" id="build-for-nodejs-amp-web-browser" style="color: inherit; text-decoration: none;">
					<h2>Build for Node.js &amp; Web Browser</h2>
				</a>
				<pre><code><span class="hljs-attr">git</span> <span class="hljs-string">clone git@github.com:compound-finance/compound-js.git</span>
<span class="hljs-attr">cd</span> <span class="hljs-string">compound-js/</span>
<span class="hljs-attr">npm</span> <span class="hljs-string">install</span>
<span class="hljs-attr">npm</span> <span class="hljs-string">run build</span></code></pre>
				<a href="#web-browser-build" id="web-browser-build" style="color: inherit; text-decoration: none;">
					<h3>Web Browser Build</h3>
				</a>
				<pre><code class="language-html"><span class="hljs-comment">&lt;!-- Local build (do `npm install` first) --&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text/javascript&quot;</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;./dist/browser/compound.min.js&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>

<span class="hljs-comment">&lt;!-- Public NPM -&gt; jsdeliver build --&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text/javascript&quot;</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;https://cdn.jsdelivr.net/npm/@compound-finance/compound-js@latest/dist/browser/compound.min.js&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span></code></pre>
				<a href="#nodejs-build" id="nodejs-build" style="color: inherit; text-decoration: none;">
					<h3>Node.js Build</h3>
				</a>
				<pre><code class="language-js"><span class="hljs-comment">// Local build (do `npm install` first)</span>
<span class="hljs-keyword">const</span> Compound = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;./dist/nodejs/index.js&#x27;</span>);

<span class="hljs-comment">// Public NPM build</span>
<span class="hljs-keyword">const</span> Compound = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;@compound-finance/compound-js&#x27;</span>);</code></pre>
			</div>
		</div>
		<div class="col-4 col-menu menu-sticky-wrap menu-highlight">
			<nav class="tsd-navigation primary">
				<ul>
					<li class="globals  ">
						<a href="globals.html"><em>Globals</em></a>
					</li>
					<li class=" tsd-kind-module">
						<a href="modules/_api_.html">&quot;api&quot;</a>
					</li>
					<li class=" tsd-kind-module">
						<a href="modules/_ctoken_.html">&quot;c<wbr>Token&quot;</a>
					</li>
					<li class=" tsd-kind-module">
						<a href="modules/_comp_.html">&quot;comp&quot;</a>
					</li>
					<li class=" tsd-kind-module">
						<a href="modules/_comptroller_.html">&quot;comptroller&quot;</a>
					</li>
					<li class=" tsd-kind-module">
						<a href="modules/_eth_.html">&quot;eth&quot;</a>
					</li>
					<li class=" tsd-kind-module">
						<a href="modules/_gov_.html">&quot;gov&quot;</a>
					</li>
					<li class=" tsd-kind-module">
						<a href="modules/_index_.html">&quot;index&quot;</a>
					</li>
					<li class=" tsd-kind-module">
						<a href="modules/_pricefeed_.html">&quot;price<wbr>Feed&quot;</a>
					</li>
					<li class=" tsd-kind-module">
						<a href="modules/_util_.html">&quot;util&quot;</a>
					</li>
				</ul>
			</nav>
			<nav class="tsd-navigation secondary menu-sticky">
				<ul class="before-current">
				</ul>
			</nav>
		</div>
	</div>
</div>
<footer>
	<div class="container">
		<h2>Legend</h2>
		<div class="tsd-legend-group">
			<ul class="tsd-legend">
				<li class="tsd-kind-function"><span class="tsd-kind-icon">Function</span></li>
			</ul>
		</div>
	</div>
</footer>
<div class="overlay"></div>
<script src="assets/js/main.js"></script>
</body>
</html>